package cn.psh.basic.controller;


import cn.psh.basic.dto.EamilRegisterDto;
import cn.psh.basic.dto.RegisterDto;
import cn.psh.basic.service.IVerifyCodeService;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;


    @GetMapping("/image/{key}")
    public AjaxResult verifyCodeimage(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImgCode(key);
            return AjaxResult.OJBK().setResultObj(base64Str);
        }catch (BusinessException e){
            return AjaxResult.OJBK().setSuccess(false).setMessage("获取失败,重新获取!"+e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }

    }

    @PostMapping("/smsCode")
    public AjaxResult phoneVerifyCode(@RequestBody RegisterDto smsCodeDto){
        try {
            verifyCodeService.phoneCode(smsCodeDto);
            return AjaxResult.OJBK();
        }catch (BusinessException e){
            return AjaxResult.OJBK().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }
    @PostMapping("/emailCode")
    public AjaxResult emailVerifyCode(@RequestBody EamilRegisterDto eamilRegisterDto){
        try {
            verifyCodeService.emailCode(eamilRegisterDto);
            return AjaxResult.OJBK();
        }catch (BusinessException e){
            return AjaxResult.OJBK().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }


    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody RegisterDto smsCodeDto) {
        try {
            verifyCodeService.binderSmsCode(smsCodeDto);
            return AjaxResult.OJBK();
        } catch (BusinessException e) {
            return AjaxResult.OJBK().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }
}
